package org.example.bean.service;

import org.example.bean.model.LoginParam;
import org.example.bean.model.LoginTypeEnum;
import org.example.bean.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserNameLoginService extends AbstractLoginService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    protected UserNameLoginService(LoginCommonStragegyRegistry loginCommonStragegyRegistry) {
        super(loginCommonStragegyRegistry);
    }

    @Override
    protected void initRegistry() {
        super.loginCommonStragegyRegistry.registry(LoginTypeEnum.USERNAMETYPE, this.getClass());
    }

    @Override
    public Result checkParam(LoginParam loginParam) {
        if (ObjectUtils.isEmpty(loginParam.getUsername())) {
            return Result.error("用户名不能为空！");
        }
        if (ObjectUtils.isEmpty(loginParam.getPassword())) {
            return Result.error("密码不能为空！");
        }
        return Result.suc();
    }

    @Override
    public Result login(LoginParam loginParam) {
        log.info("用户名登录开始...");
        if (loginParam.getUsername().equals("admin") && loginParam.getPassword().equals("123456")) {
            return Result.suc("登录成功！" + "token_admin_123456"); // 假设登录成功返回一个token。
        } else {
            return Result.error("用户名或密码错误！"); // 假设用户名或密码错误。
        }

    }

}
